<template>
    <div class="nest">
        <el-form class="search-form" inline size="mini">
            <el-form-item>
                <el-input v-model="search.title" placeholder="作品名" clearable @change="getList" />
            </el-form-item>
            <el-form-item>
                <el-input v-model="search.user_id" placeholder="用户id" clearable @change="getList" />
            </el-form-item>
            <el-form-item>
                <el-select v-model="search.category_id" placeholder="分类" clearable @change="getList">
                    <el-option v-for="(v, i) in CategoryList" :key="i" :label="v.title" :value="v.id" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-select v-model="search.status" placeholder="状态" clearable @change="getList">
                    <el-option label="待审核" value="0" />
                    <el-option label="已发布" value="1" />
                    <el-option label="审核失败" value="2" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="getList">查询</el-button>
                <el-button icon="el-icon-refresh-left" size="mini" @click="handleResetSearch">重置</el-button>
            </el-form-item>
            <add-nest ref="addNest" @success="getList"></add-nest>
        </el-form>
        <div class="table">
            <el-table :data="tableData" style="width: 100%;margin-bottom: 20px;" v-loading="loading">
                <el-table-column prop="id" label="ID" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="image" label="作品头像" show-overflow-tooltip>
                    <template slot-scope="scope">
                        <el-image :src="scope.row.image" :preview-src-list="[scope.row.image]" style="width: 60px" />
                    </template>
                </el-table-column>
                <el-table-column prop="title" label="作品名称" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="category.title" label="作品分类" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="type" label="类型" show-overflow-tooltip>
                    <template slot-scope="scope">
                        {{ scope.row.type == 1 ? "图片" : scope.row.type == 2 ? "视频" : "位置" }}
                    </template>
                </el-table-column>
                <el-table-column prop="status" label="状态" show-overflow-tooltip>
                    <template slot-scope="scope">
                        {{ scope.row.status == 1 ? "已发布" : scope.row.status == 2 ? "审核失败" : "待审核" }}
                    </template>
                </el-table-column>
                <el-table-column label="各端显示情况" show-overflow-tooltip>
                    <template slot-scope="{row}">
                        <div>
                            <span style="display: inline-block;width: 30px;">H5</span>
                            <el-switch v-model="row.h5_show" :active-value="0" :inactive-value="1"
                                @change="setShowStatus(row, 'h5_show')">
                            </el-switch>
                        </div>
                        <div>
                            <span style="display: inline-block;width: 30px;">微信</span>
                            <el-switch v-model="row.wx_show" :active-value="0" :inactive-value="1"
                                @change="setShowStatus(row, 'wx_show')">
                            </el-switch>
                        </div>
                        <div>
                            <span style="display: inline-block;width: 30px;">抖音</span>
                            <el-switch v-model="row.dy_show" :active-value="0" :inactive-value="1"
                                @change="setShowStatus(row, 'dy_show')">
                            </el-switch>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="reward_money" label="广告收益金额" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="activity_money" label="活动奖励金额" show-overflow-tooltip>
                </el-table-column>
                <el-table-column label="作者信息" show-overflow-tooltip>
                    <template slot-scope="scope">
                        <div style="display: flex;flex-direction: column;">
                            <el-image :src="scope.row.user.avatar" :preview-src-list="[scope.row.user.avatar]"
                                style="width: 60px;border-radius: 50%;" />
                            <div>{{ scope.row.user.nickname }}</div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="created_at" label="创建时间" show-overflow-tooltip>
                </el-table-column>
                <el-table-column label="操作" width="200">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini"
                            @click="$refs.addNest.handleEdit(scope.row)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="nestDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div style="text-align: right;">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="search.page" :page-sizes="[10, 20, 30, 40, 100]" :page-size="search.size" background
                    layout="total, sizes, prev, pager, next" :total="total">
                </el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import addNest from "./addNest.vue"

export default {
    components: { addNest },
    data() {
        return {
            loading: false,
            search: {
                title: "",	//false	string		作品名
                category_id: "",	//false	int		分类id
                user_id: "",//false	int		分类id
                status: "",	//true	int		-1全部 0待审核 1发布中 2审核失败
                page: 1,	//true	int		当前页
                size: 10,	//true	int		每页多少条
            },
            tableData: [],
            total: 0,
            CategoryList: []
        }
    },
    created() {
        if (this.$route.query.user_id) {
            this.search.user_id = this.$route.query.user_id
        }
        this.getList()
        this.getCategoryList()
    },
    methods: {
        // 更新作品显示状态
        async setShowStatus(data, field) {
            let res = await this.$request({
                url: '/admin/work.list/setShowStatus',
                method: 'post',
                data: { id: data.id, field }
            })
            // console.log(res);
        },
        // 删除
        nestDelete(data) {
            this.$confirm(`确认删除作品【${data.title}】吗？`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                let res = await this.$request({
                    url: '/admin/work.list/delete',
                    method: 'post',
                    data: { id: data.id }
                })
                if (res.code == 200) {
                    this.$message.success("删除成功")
                    this.getList()
                }

            })
        },
        // 分类列表
        async getCategoryList() {
            let res = await this.$request({
                url: '/admin/work.category/index',
                method: 'get',
                params: {
                    title: "",	//true	string		名称
                    status: "",	//true	string		状态 0 全部 1 启用 2 禁用
                    page: 1,	//true	int		当前页
                    size: 100,	//true	int		每页多少条
                }
            })
            if (res.code == 200) {
                this.CategoryList = res.result.data
            }
        },
        // 获取列表
        async getList() {
            this.loading = true
            let res = await this.$request({
                url: '/admin/work.list/index',
                method: 'get',
                params: this.search
            })
            this.loading = false
            if (res.code == 200) {
                this.tableData = res.result.data
                this.total = res.result.total
            }
        },
        handleResetSearch() {
            this.search = {
                title: "",	//false	string		作品名
                category_id: "",	//false	int		分类id
                user_id: "",//false	int		分类id
                status: "",	//true	int		-1全部 0待审核 1发布中 2审核失败
                page: 1,	//true	int		当前页
                size: 10,	//true	int		每页多少条
            }
        },
        //分页
        handleSizeChange(e) {
            this.search.size = e;
            this.getList()
        },
        handleCurrentChange(e) {
            this.search.page = e;
            this.getList()
        },
        updateReIntegralTask(data) {
            console.log(data);

        },
        handleSearch() {

        },
    }
}
</script>

<style lang="scss">
.nest {
    width: 100%;
    height: 60px;

    .search-form {
        background: #fff;
        border-radius: 12px;
        padding: 12px 20px;
        margin-bottom: 20px;

        .el-form-item--mini.el-form-item {
            margin-bottom: 0;
        }
    }

    .table {
        padding: 12px 20px;
        background: #fff;

        .el-table__body-wrapper {
            max-height: calc(100vh - 300px);
            overflow-y: auto;
        }
    }
}
</style>
